package org.yy4ever.smartremind;

import org.yy4ever.smartremind.util.SimpleHttpClient;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Login extends Activity {
	
	final static String REMIND= "smartremind";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final EditText login_name = (EditText)findViewById(R.id.login_name);
        final EditText login_password = (EditText)findViewById(R.id.login_password);
        Button login_submit = (Button)findViewById(R.id.login_submit);
        Button login_reg = (Button)findViewById(R.id.login_reg);
        
        String email = getSharedPreferences(REMIND, 0).getString("email", "");
        if(!email.equals(""))
        	login_name.setText(email);
        
        login_reg.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("https://www.google.com/accounts/Login?hl=zh-CN"));
				startActivity(intent);
			}
		});
        login_submit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String output = new SimpleHttpClient().doClientLogin(login_name.getText().toString().trim()
						, login_password.getText().toString().trim());
				if(output.contains("Error Response")){
					login_name.setText("");
					login_password.setText("");
				}else{
					Editor editor = getSharedPreferences(REMIND, 0).edit();
					editor.putString("email", login_name.getText().toString().trim());
					editor.commit();
					Intent intent = new Intent(Login.this,SmartRemindListView.class);
					startActivity(intent);
				}
			}
		});
    }
    
}